﻿using AutoMapper;
using MediatR;
using SC.GirdManage.Common;
using SC.GirdManage.Repository;
using SC.GridManage.Domain.Models;
using SC.GridManage.MN.API.Read.Application.Command;

namespace SC.GridManage.MN.API.Read.Application.Handler
{
    public class QueryAtricalHandler : IRequestHandler<QueryArticalCommand, ApiResult<ApiPaging<QueryArticalDTO>>>
    {
        private readonly IBaseRepository<ArticalModel> articalRepo;
        private readonly IMapper mapper;
        private readonly IBaseRepository<ArticalStatusModel> statusRepo;
        private readonly IBaseRepository<ArticalDepartModel> departRepo;
        private readonly IBaseRepository<ArticalTypeModel> typeRepo;
        private readonly IBaseRepository<ArticalUserModel> userRepo;

        public QueryAtricalHandler(IBaseRepository<ArticalModel> articalRepo,IMapper mapper, IBaseRepository<ArticalStatusModel> statusRepo, IBaseRepository<ArticalDepartModel> departRepo, IBaseRepository<ArticalTypeModel> typeRepo, IBaseRepository<ArticalUserModel> userRepo)
        {
            this.articalRepo = articalRepo;
            this.mapper = mapper;
            this.statusRepo = statusRepo;
            this.departRepo = departRepo;
            this.typeRepo = typeRepo;
            this.userRepo = userRepo;
        }

        public Task<ApiResult<ApiPaging<QueryArticalDTO>>> Handle(QueryArticalCommand request, CancellationToken cancellationToken)
        {
            //返回响应值
            ApiResult<ApiPaging<QueryArticalDTO>> result = new ApiResult<ApiPaging<QueryArticalDTO>>();

            var articals = articalRepo.QueryAsync().Result.Where(x => x.IsDeleted == false); //文章的仓储拿到文章列表
            if (!string.IsNullOrEmpty(request.Title)) {//文章标题的模糊查询
                articals = articals.Where(x=>x.Title.Contains(request.Title));
            }

            var status = statusRepo.QueryAsync().Result;//他是一个Task ,所以要有Result
            var departs = departRepo.QueryAsync().Result;//他是一个Task ,所以要有Result
            var types = typeRepo.QueryAsync().Result;//他是一个Task ,所以要有Result
            var users = userRepo.QueryAsync().Result;//他是一个Task ,所以要有Result

            var linq = from a in articals
                       join s in status on a.StatusID equals s.Id
                       join t in types on a.TypeId equals t.Id
                       join u in users on a.UserId equals u.Id
                       join d in departs on a.DepartId equals d.Id
                       select new QueryArticalDTO
                       {
                           Id = a.Id,
                           TypeId = a.TypeId,
                           TypeName = t.TypeName,
                           Title = a.Title,
                           StatusID = a.StatusID,
                           StatusName  = s.StatusName,
                           UserId = a.UserId,
                           UserName = u.UserName,
                           DepartId = d.Id,
                           DepartName = d.DepartName,
                           Link = a.Link,
                           Content = a.Content,
                           IsTop = a.IsTop
                       };
            var count = linq.Count();

            var data = linq.OrderByDescending(x => x.IsTop).OrderByDescending(x=>x.Id)
                .Skip((request.Index - 1) * request.Size)
                .Take(request.Size).ToList();

            //分页包装
            ApiPaging<QueryArticalDTO> page = new ApiPaging<QueryArticalDTO>();
            page.Data = data;//分页的数据
            page.TotalCount = count; //总条数
            page.TotalPage = (int)Math.Ceiling(count * 1.0 / request.Size);//总页数


            result.Data = page;//Data需要的是分页包装
            result.Code=GirdManage.Common.Enums.EnumResultCode.Success;
            result.Msg = "分页查寻成功";

            return Task.FromResult(result);
        }
    }
}
